// ID: B0201
package git.bankview.wl.servlet;

import java.io.*;
import java.util.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import org.apache.log4j.Logger;

import laya.util.*;

import git.bankview.sl.Globals;
import git.bankview.sl.report.ReportParameter;
import git.bankview.sl.report.ReportSchd;
import git.bankview.sl.report.SchdUtil;

/**
 *
 * @version v0.00.0024
 * @author BJ L.Shuang(2004/05/07)
 */
public abstract class SingleFileDownloadServlet extends HttpServlet
{
	private static Logger log = Logger.getLogger(SingleFileDownloadServlet.class);

	/**
	 *
	 * @param request Request
	 * @param response Response
	 * @see #doGet(HttpServletRequest,HttpServletResponse)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		File exportfile = resolveRequestedFile(request);

        response.setContentType( "application/unknown; charset=iso8859_1" );
        response.addHeader( "content-disposition", "attachment;filename=" + exportfile.getName());
        InputStream file = new FileInputStream(exportfile);
        OutputStream out = response.getOutputStream();

        byte[] buffer = new byte[1024];
        int length = 0;
        for (; (length = file.read(buffer, 0, buffer.length)) > -1;)
        {
            out.write(buffer, 0, length);
        }
        
        file.close();
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		doGet(request, response);
	}
	
	protected abstract File resolveRequestedFile(HttpServletRequest request)
		throws ServletException, IOException;
}
